package com.ta.gulimall.product.controller;

import com.ta.gulimall.common.utils.system.PageUtils;
import com.ta.gulimall.common.utils.system.R;
import com.ta.gulimall.product.service.AttrGroupService;
import com.ta.gulimall.product.view.entity.AttrEntity;
import com.ta.gulimall.product.view.entity.AttrGroupEntity;
import com.ta.gulimall.product.view.vo.AttrGroupVo;
import com.ta.gulimall.product.view.vo.AttrGroupVoResponse;
import com.ta.gulimall.product.view.vo.AttrgroupWithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author tuao
 * @email tuaoo@gmail.com
 * @date 2022-10-29 01:29:17
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 回显属性组关联下的属性值
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R relationAttrgroup(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntities = attrGroupService.getAttrs(attrgroupId);

        return R.ok().put("data", attrEntities);
    }

    /**
     * 分页属性组的列表
     * @param params
     * @param catelogId
     * @return
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 回显修改属性组的信息
     * @param attrGroupId
     * @return
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        AttrGroupVoResponse attrGroup = attrGroupService.getAttrGroup(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 查询属性组下没有被关联的属性
     * @param params
     * @param attrGroupId
     * @return
     */
    @RequestMapping("/{attrGroupId}/noattr/relation")
    public R noattrRelation(@RequestParam Map<String, Object> params, @PathVariable("attrGroupId") Long attrGroupId){
        PageUtils page = attrGroupService.pageAttrNoRelation(params, attrGroupId);

        return R.ok().put("page", page);
    }

    /**
     * 删除属性组关联的属性
     * @param attrGroupVos
     * @return
     */
    @RequestMapping("/attr/relation/delete")
    public R delete(@RequestBody List<AttrGroupVo> attrGroupVos){
        attrGroupService.deleteAttrGroupRelevances(attrGroupVos);

        return R.ok();
    }

    /**
     * 关联属性组下的属性
     * @param attrGroupVos
     * @return
     */
    @PostMapping ("/attr/relation")
    public R save(@RequestBody List<AttrGroupVo> attrGroupVos){
		attrGroupService.insertRelationAttrs(attrGroupVos);

        return R.ok();
    }

    /**
     * 分类下的属性组集合与属性值集合
     */
    @RequestMapping("/{catelogId}/withattr")
    public R attrgroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrgroupWithAttrVo> attrgroupWithAttrVos = attrGroupService.attrgroupWithAttrs(catelogId);

        return R.ok().put("data",attrgroupWithAttrVos);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);
        return R.ok();
    }

}
